package com.gopay.checkorder.service.task;

import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.apache.commons.lang3.time.StopWatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.checkorder.dao.BankAuthOrderDAO;
import com.gopay.checkorder.service.AdPayAuthOrderService;
import com.gopay.checkorder.service.task.thread.AdpayAuthCheckOrderTaskThread;
import com.gopay.common.domain.bank.BankAuthOrder;

/**
 * 鉴权查询定时任务
 * 
 * @CheckAdPayAuthOrderTaskImpl.java
 * @author lilili
 * @2017-1-13 上午11:43:46 www.gopay.com.cn Inc.All rights reserved.
 */
public class AdPayAuthCheckOrderTaskImpl implements Task {

	private final static Logger logger = LoggerFactory.getLogger("CHECK_ORDER");

	private static final int MAX_THREAD_COUNT = 10;

	private ExecutorService threadPool = Executors.newFixedThreadPool(MAX_THREAD_COUNT);

	/**
	 * 银行流水状态
	 */
	private String orderStatus;

	/**
	 * 对单次数
	 */
	private int totalCheckTimes;

	@Resource(name = "bankAuthOrderDAO")
	private BankAuthOrderDAO bankAuthOrderDAO;

	@Resource(name = "adPayAuthOrderService")
	private AdPayAuthOrderService adPayAuthOrderService;

	private String BANK = "CMBC";

	private void printLog(StopWatch sw, String bank, String orderStatus, int totalCheckTimes, String prefix) {
		sw.split();
		logger.error(prefix + "------,bank=" + BANK + ",orderStatus=" + orderStatus + ",totalCheckTimes=" + totalCheckTimes + ",耗时=" + sw.getSplitTime());
	}

	@Override
	public void doTask() {
		try {
			StopWatch sw = new StopWatch();
			sw.start();
			printLog(sw, BANK, orderStatus, totalCheckTimes, "AdPayAuthCheckOrderTaskImpl------start");

			int pageSize = 300;
			int pageIndex = 1;

			List<BankAuthOrder> list = getCheckOrder(orderStatus, totalCheckTimes, pageIndex, pageSize);
			printLog(sw, BANK, orderStatus, totalCheckTimes, "AdPayAuthCheckOrderTaskImpl------list.size()=" + list.size() + ",pageIndex=" + pageIndex);

			// 订单队列
			ConcurrentLinkedQueue<BankAuthOrder> queue = new ConcurrentLinkedQueue<BankAuthOrder>();
			queue.addAll(list);

			// 同步锁存器
			CountDownLatch latch = new CountDownLatch(queue.size());

			// 线程池中线程全部参与
			for (int i = 0; i < MAX_THREAD_COUNT; i++) {
				AdpayAuthCheckOrderTaskThread thread = new AdpayAuthCheckOrderTaskThread(adPayAuthOrderService, queue, latch);
				threadPool.execute(thread);

			}

			// 同步锁存器等待所有线程执行完成
			if (!latch.await(118, TimeUnit.SECONDS)) {
				logger.error("同步锁存器等待超时了 latch.count=" + latch.getCount());
				threadPool.awaitTermination(1, TimeUnit.SECONDS);
			}

			printLog(sw, BANK, orderStatus, totalCheckTimes, "AdPayAuthCheckOrderTaskImpl------end");
			sw.stop();
		} catch (Throwable t) {
			logger.error("执行对单Task时出现异常", t);
		}
	}

	/**
	 * 分页查询对账记录
	 * 
	 * @param orderStatus
	 * @param totalCheckTimes
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	protected List<BankAuthOrder> getCheckOrder(String orderStatus, int totalCheckTimes, int pageIndex, int pageSize) {

		return bankAuthOrderDAO.getCheckOrder(orderStatus, totalCheckTimes, pageIndex, pageSize);

	}

	public String getOrderStatus() {
		return orderStatus;
	}

	public void setOrderStatus(String orderStatus) {
		this.orderStatus = orderStatus;
	}

	public int getTotalCheckTimes() {
		return totalCheckTimes;
	}

	public void setTotalCheckTimes(int totalCheckTimes) {
		this.totalCheckTimes = totalCheckTimes;
	}

}
